﻿namespace Yipusoft.Account.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Yipusoft.Account.DBUtility;
    using Yipusoft.Account.Model;

    public class Department
    {
        public bool Delete(string DepartmentID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = DepartmentID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Departments_Delete", commandParameters) > 0);
            }
        }

        public bool Exsits(string DepartmentID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = DepartmentID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Departments_Exsits", commandParameters).Read();
            }
        }

        public DepartmentInfo GetDepartment(string DepartmentID)
        {
            DepartmentInfo info = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = DepartmentID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Departments_GetDetail", commandParameters);
                if (reader.Read())
                {
                    info = new DepartmentInfo();
                    info.DepartmentID = reader.GetString(0);
                    info.FullName = reader.GetString(1);
                    info.ShortName = reader.GetString(2);
                    info.AliasName = reader.GetString(3);
                    info.ParentID = reader.GetString(4);
                    info.State = reader.GetInt32(5);
                }
                return info;
            }
        }

        public List<DepartmentInfo> GetList(string StrWhere)
        {
            List<DepartmentInfo> list = new List<DepartmentInfo>();
            DepartmentInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.NVarChar, 0xfa0) };
            commandParameters[0].Value = StrWhere;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Departments_GetList", commandParameters);
                while (reader.Read())
                {
                    item = new DepartmentInfo();
                    item.DepartmentID = reader.GetString(0);
                    item.FullName = reader.GetString(1);
                    item.ShortName = reader.GetString(2);
                    item.AliasName = reader.GetString(3);
                    item.ParentID = reader.GetString(4);
                    item.State = reader.GetInt32(5);
                    list.Add(item);
                }
                return list;
            }
        }

        public DepartmentInfo GetOADepartment(string DepartmentID)
        {
            string commandText = " select * from dwbm where dwid='" + DepartmentID.Substring(2, 5) + "'";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.CONN_STRING_OA, CommandType.Text, commandText))
            {
                if (reader.Read())
                {
                    DepartmentInfo info = new DepartmentInfo();
                    info.DepartmentID = DepartmentID;
                    info.FullName = reader["name"].ToString();
                    info.ShortName = reader["name"].ToString();
                    info.AliasName = "";
                    info.ParentID = "";
                    info.State = 0;
                    return info;
                }
                return null;
            }
        }

        public bool Insert(DepartmentInfo Department)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.NVarChar, 50), new SqlParameter("@FullName", SqlDbType.NVarChar, 50), new SqlParameter("@ShortName", SqlDbType.NVarChar, 50), new SqlParameter("@AliasName", SqlDbType.NVarChar, 50), new SqlParameter("@ParentID", SqlDbType.NVarChar, 50), new SqlParameter("@State", SqlDbType.Int) };
            commandParameters[0].Value = Department.DepartmentID;
            commandParameters[1].Value = Department.FullName;
            commandParameters[2].Value = Department.ShortName;
            commandParameters[3].Value = Department.AliasName;
            commandParameters[4].Value = Department.ParentID;
            commandParameters[5].Value = Department.State;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Departments_Insert", commandParameters) > 0);
            }
        }

        public bool Update(DepartmentInfo Department)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.NVarChar, 50), new SqlParameter("@FullName", SqlDbType.NVarChar, 50), new SqlParameter("@ShortName", SqlDbType.NVarChar, 50), new SqlParameter("@AliasName", SqlDbType.NVarChar, 50), new SqlParameter("@ParentID", SqlDbType.NVarChar, 50), new SqlParameter("@State", SqlDbType.Int) };
            commandParameters[0].Value = Department.DepartmentID;
            commandParameters[1].Value = Department.FullName;
            commandParameters[2].Value = Department.ShortName;
            commandParameters[3].Value = Department.AliasName;
            commandParameters[4].Value = Department.ParentID;
            commandParameters[5].Value = Department.State;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Departments_Update", commandParameters) > 0);
            }
        }
    }
}

